package cn.tedu.lbs.basic.web.service;

import cn.tedu.lbs.basic.web.repository.AdminRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.passport.encoder.JwtEncoder;
import com.tarena.lbs.common.passport.enums.Roles;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.passport.param.AdminLoginParam;
import com.tarena.lbs.pojo.passport.vo.LoginVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;

    public LoginVO doAdminLogin(AdminLoginParam adminLoginParam) throws BusinessException {
        //1.使用参数phone 查询adminPO
        AdminPO adminPO=checkAndGetAdminByPhone(adminLoginParam.getPhone());
        //2.检查提交参数密码 和数据库密码是否一致
        checkPasswordMatch(adminLoginParam.getPassword(),adminPO.getAccountPassword());
        //3.生成一个JWT
        String jwt=generateJwt(adminPO);
        //4.封装loginVO
        LoginVO loginVO=new LoginVO(jwt,adminPO.getId(),adminPO.getNickname());
        return loginVO;
    }

    private void checkPasswordMatch(String password, String accountPassword) throws BusinessException {
        //入参两个值 不能是空
        Asserts.isTrue(
                StringUtils.isEmpty(password)||StringUtils.isEmpty(accountPassword),
                new BusinessException("-2","提交密码或者保存密码为空"));
        //不相等
        Asserts.isTrue(!StringUtils.equals(password,accountPassword),
                new BusinessException("-2","密码不正确"));
    }

    private AdminPO checkAndGetAdminByPhone(String phone) throws BusinessException {
        //select * from lbs_admin where account_phone=? name like (like in 等)
        QueryWrapper<AdminPO> query=new QueryWrapper();
        query.eq("account_phone",phone);
        AdminPO po = adminRepository.getOne(query);
        //判断po是否为空
        //但满足异常的逻辑表达式的时候 比如 对象为空 比如参数长度不足 抛出异常 可以利用断言代替
        /*if (po==null){
            //电话号码不存在 抛异常RuntimeException(message) 自定义业务异常 其他异常 算作是系统异常
            throw new BusinessException("-2","电话号码不存在");
        }*/
        Asserts.isTrue(po==null,new BusinessException("-2","电话号码不存在"));
        return po;
    }
    //TODO 方法考虑复用
    private String generateJwt(AdminPO adminPO) throws BusinessException {
        //1.jwt携带的数据 不可以是adminPO 业务领域边界 PO 只针对数据库
        UserPrinciple userPrinciple=new UserPrinciple();
        BeanUtils.copyProperties(adminPO,userPrinciple);
        //利用adminPO的数据 封装认证对象 id和nickname直接拷贝
        userPrinciple.setRole(adminPO.getAccountType()==0? Roles.ADMIN:Roles.SHOP);
        //2.缺少生成转化jwt的工具 系统中有三个角色Roles.ADMIN(平台) Roles.SHOP(商家) Roles.USER(小程序)
        /*JwtEncoder jwtEncoder=new JwtEncoder();
        jwtEncoder.setSecret("123456");*/
        return jwtEncoder.generateToken(userPrinciple);
    }
}
